<template>
  <div>
    <nav>
      <ul>
        <li>
          全部(20)
          <span></span>
        </li>
        <li>
          降价(4)
          <span></span>
        </li>
        <li>
          常卖(2)
          <span></span>
        </li>
        <li>
          分类
          <img src="../../../assets/img/购物车 (1).png" alt="#" />
        </li>
      </ul>
    </nav>
    <main>
      <div class="content1" v-for="item in arr" :key="item.id">
        <input type="checkbox" v-model="item.flag" @change="change()" />
        <img :src="$pre+item.img" alt="#" class="tu2" />
        <ul>
          <li>{{item.goodsname}}</li>
          <li class="kuan"></li>
          <li></li>
          <li></li>
          <li>
            <div>
              <span id="ppp">{{item.price}}￥</span>
            </div>
            <div>
              <img src="../../../assets/img/购物车 (5).png" alt="#" @click="sub(item)" />
              <p>{{item.num}}</p>
              <img src="../../../assets/img/购物车 (6).png" alt="#" @click="add(item.id)" />
            </div>
            <div>
              <van-button type="warning" @click="del(item.id)">删除</van-button>
            </div>
          </li>
        </ul>
      </div>
    </main>
    <footer>
      <div class="top">
        <ul>
          <li>
            <input type="checkbox" @change="all" v-model="allFlag" />
          </li>
          <li>全选</li>
          <li>
            <p>合计</p>
            <p>已免运费</p>
          </li>
          <li>
            <p>￥{{price}}</p>
          </li>
        </ul>
        <div @click="toOrder">结算</div>
      </div>
    </footer>
  </div>
</template>

<script>
import { reqShopList, reqShopEdit, reqShopDel } from "../../../http/api";
export default {
  data() {
    return {
      allFlag: false,
      arr: [],
      isSub: true
    };
  },
  mounted() {
    this.req();
  },
  methods: {
    toOrder() {
      // countmoney:总价 countnumber：总数量 idstr：选中的id的字符串 1,2,3
      let str = this.arr.reduce((val, item) => (item.flag ? (val += item.id + ",") : val),"");
      let json = {
        countmoney: this.price,
        countnumber: this.arr.reduce((val, item) => (item.flag ? (val += item.num) : val),0),
        idstr: str.slice(0, str.length - 1)
      };
      localStorage.setItem("order", JSON.stringify(json));
      this.$router.push("/confirm?");
    },
    req() {
      let old = [...this.arr];
      reqShopList({
        uid: JSON.parse(localStorage.getItem("user")).uid,
        authorization: JSON.parse(localStorage.getItem("user")).token
      }).then(res => {
        res.data.list.forEach(item => {
          item.flag = old.find(i => i.id == item.id)? old.find(i => i.id == item.id).flag: false;
        });
        this.arr = res.data.list;
        if (res.data.code == 200) {
          this.isSub = true;
          this.allFlag = this.arr.every(item => item.flag);
        }
      });
    },
    all() {
      this.arr.forEach(item => {
        item.flag = this.allFlag;
      });
      console.log(this.arr);
    },
    change() {
      this.allFlag = this.arr.every(item => item.flag);
    },
    add(id) {
      reqShopEdit({
        id: id,
        type: 2,
        authorization: JSON.parse(localStorage.getItem("user")).token
      }).then(res => {
        this.req();
      });
    },
    sub(e) {
      if (this.isSub && e.num > 1) {
        this.isSub = false;
        reqShopEdit({
          id: e.id,
          type: 1,
          authorization: JSON.parse(localStorage.getItem("user")).token
        }).then(res => {
          this.req();
        });
      }
    },
    del(id) {
      reqShopDel({
        id: id,
        authorization: JSON.parse(localStorage.getItem("user")).token
      }).then(res => {
        this.req();
      });
    }
  },
  computed: {
    price() {
      let sum = this.arr.reduce((val, item) => {
        if (item.flag) {
          val = val + item.price * item.num;
          return val;
        } else {
          return val;
        }
      }, 0);
      return sum;
    }
  }
};
</script>

<style scoped>
.kuan {
  width: 50vw;
}
nav {
  background: linear-gradient(to bottom, #ff6445, #ff8679);
  height: 0.6rem;
}
nav ul {
  display: flex;
  justify-content: space-around;
}
nav ul li {
  width: 1.3rem;
  font-size: 0.26rem;
  line-height: 0.56rem;
  color: white;
  text-align: center;
}
nav ul li span {
  display: block;
  width: 0rem;
  height: 0.04rem;
  border-radius: 2px;
  background-color: white;
  margin: 0 auto;
  transition: all 0.5s;
}
nav ul li:nth-of-type(4) {
  display: flex;
  align-items: center;
  justify-content: center;
}
nav ul li:nth-of-type(4) img {
  width: 0.14rem;
  height: 0.26rem;
  margin-left: 0.1rem;
}
nav ul li:hover span {
  width: 0.4rem;
}
main {
  background-color: #f2f2f2;
  margin-bottom: 1.5rem;
}
main .top1 {
  background-color: white;
  height: 0.72rem;
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 0.2rem;
}
main .top1 p:nth-of-type(1) {
  font-size: 0.3rem;
  font-weight: bolder;
  line-height: 0.72rem;
}
main .top1 p:nth-of-type(1) input {
  margin: 0 0.2rem;
}
main .top1 p:nth-of-type(2) {
  font-size: 0.22rem;
}
main .top1 p:nth-of-type(2) span {
  padding: 0 0.2rem;
}
main .top1 p:nth-of-type(2) span:nth-of-type(2) {
  border-left: 1px solid gray;
  color: #ff6445;
}
main .content1 {
  background-color: white;
  height: 2.6rem;
  padding: 0.2rem 0.1rem;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
main .content1 .tu1 {
  width: 0.32rem;
  height: 0.32rem;
  margin: 0 0.2rem;
}
main .content1 .tu2 {
  width: 2.16rem;
  height: 2.16rem;
  margin-right: 0.2rem;
}
main .content1 ul li:nth-of-type(1) {
  font-size: 0.3rem;
  margin-bottom: 0.1rem;
}
main .content1 ul li:nth-of-type(2) {
  font-size: 0.28rem;
  color: gray;
  margin-bottom: 0.3rem;
}
main .content1 ul select {
  background-color: #f2f2f2;
  border: none;
  font-size: 0.2rem;
  line-height: 0.32rem;
}
main .content1 ul li:nth-of-type(4) {
  margin-top: 0.1rem;
  font-size: 0.2rem;
  color: #ff8679;
}
main .content1 ul li:nth-of-type(5) {
  margin-top: 0.2rem;
  display: flex;
  justify-content: space-between;
}
#ppp {
  font-size: 0.16rem;
  color: #ff6445;
}
main .content1 ul li:nth-of-type(5) span:nth-of-type(2) {
  font-size: 0.3rem;
  color: #ff6445;
}
main .content1 ul li:nth-of-type(5) span:nth-of-type(2) {
  font-size: 0.16rem;
  color: gray;
  text-decoration: line-through;
}
main .content1 ul li:nth-of-type(5) div {
  display: flex;
  height: 0.4rem;
  align-items: center;
}
main .content1 ul li:nth-of-type(5) div p {
  text-align: center;
  line-height: 0.4rem;
  width: 0.8rem;
  background-color: #f2f2f2;
  border-radius: 2px;
}
main .content1 ul li:nth-of-type(5) div img {
  width: 0.51rem;
  height: 0.4rem;
}
main .top2 {
  background-color: white;
  height: 0.72rem;
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin: 0.2rem 0;
}
main .top2 p:nth-of-type(1) {
  font-size: 0.3rem;
  font-weight: bolder;
  line-height: 0.72rem;
}
main .top2 p:nth-of-type(1) input {
  margin: 0 0.2rem;
}
main .top2 p:nth-of-type(2) {
  font-size: 0.22rem;
}
main .top2 p:nth-of-type(2) span {
  padding: 0 0.2rem;
}
main .top2 p:nth-of-type(2) span:nth-of-type(2) {
  border-left: 1px solid gray;
  color: #ff6445;
}
footer {
  margin-bottom: 0.65rem;
  position: fixed;
  bottom: 0;
  left: 0;
  background-color: white;
}
footer .top {
  border-top: 1px gainsboro solid;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
footer .top ul {
  width: 5.3rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
}
footer .top ul input {
  margin: 0 0.2rem;
}
footer .top ul li:nth-of-type(2) {
  font-size: 0.3rem;
  font-weight: bolder;
}
footer .top ul li:nth-of-type(3) p:nth-of-type(1) {
  font-size: 0.3rem;
  font-weight: bolder;
  margin-bottom: 0.1rem;
}
footer .top ul li:nth-of-type(3) p:nth-of-type(2) {
  font-size: 0.24rem;
  color: gray;
}
footer .top ul li:nth-of-type(4) {
  text-align: right;
  margin-right: 0.2rem;
}
footer .top ul li:nth-of-type(4) p:nth-of-type(1) {
  font-size: 0.3rem;
  font-weight: bolder;
  color: #ff6040;
  margin-bottom: 0.1rem;
}
footer .top ul li:nth-of-type(4) p:nth-of-type(2) {
  font-size: 0.24rem;
  color: gray;
}
footer .top div {
  background-color: #ff6040;
  font-size: 0.3rem;
  color: white;
  font-weight: bolder;
  width: 2.2rem;
  height: 0.98rem;
  line-height: 0.98rem;
  text-align: center;
}
</style>